package com.example.myapplication.tack_21.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.tack_21.DectorActivity;
import com.example.myapplication.tack_21.DectorXQActivity;
import com.example.myapplication.tack_21.data.DectorData;
import com.example.myapplication.tack_21.data.FenLeiData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.google.gson.Gson;

import java.util.List;

public class DectorAdapter extends RecyclerView.Adapter<DectorAdapter.My> {
    private List<DectorData.RowsDTO> list;
    private Context context;
    private View view;

    public DectorAdapter(List<DectorData.RowsDTO> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public My onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(context).inflate(R.layout.tack_21_dector,null);
        return new My(view);
    }

    @Override
    public void onBindViewHolder(@NonNull My holder, int position) {
        Glide.with(context).load(OkHttpTool.URL + list.get(position).getAvatar()).into(holder.img);
        holder.name.setText(list.get(position).getName());
        holder.jobName.setText(list.get(position).getJobName());
        if(!TextUtils.isEmpty(list.get(position).getPracticeNo())){
            holder.practiceNo.setText(list.get(position).getPracticeNo());
        }else{
            holder.practiceNo.setText("无");
        }

        String substring = list.get(position).getGoodAt().substring(0, 10)+"...";
        holder.goodAt.setText(substring);
        holder.line.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, DectorXQActivity.class);
                DectorData.RowsDTO rowsDTO = new DectorData.RowsDTO(list.get(position).getTypeId(),list.get(position).getAvatar());
                Gson gson = new Gson();
                String json = gson.toJson(rowsDTO);
                intent.putExtra(Common.COMMON,json);
                context.startActivity(intent);

            }
        });
    }


    @Override
    public int getItemCount() {
        return list != null?list.size():0;
    }

    public class My extends RecyclerView.ViewHolder{
        private ImageView img;
        private TextView name;
        private TextView jobName;
        private TextView practiceNo;
        private TextView goodAt;
        private LinearLayout line;
        public My(@NonNull View itemView) {
            super(itemView);
            img = view.findViewById(R.id.img);
            name = view.findViewById(R.id.name);
            practiceNo = view.findViewById(R.id.practiceNo);
            jobName = view.findViewById(R.id.jobName);
            goodAt = view.findViewById(R.id.goodAt);
            line = view.findViewById(R.id.line);
        }
    }
}
